package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.AdvertDAO;
import com.hcrazy.manchester.app.jdbc.BookFoodDAO;
import com.hcrazy.manchester.app.jdbc.CommentDAO;
import com.hcrazy.manchester.app.jdbc.PraiseDAO;
import com.hcrazy.manchester.app.pojo.Advert;
import com.hcrazy.manchester.app.pojo.BookFood;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class BookFoodService {
	
	private PraiseDAO praiseDAO;
	
	private AdvertDAO advertDAO;
	
	public AdvertDAO getAdvertDAO() {
		return advertDAO;
	}

	public void setAdvertDAO(AdvertDAO advertDAO) {
		this.advertDAO = advertDAO;
	}

	public PraiseDAO getPraiseDAO() {
		return praiseDAO;
	}

	public void setPraiseDAO(PraiseDAO praiseDAO) {
		this.praiseDAO = praiseDAO;
	}

	private CommentDAO commentDAO;

	public CommentDAO getCommentDAO() {
		return commentDAO;
	}

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	private BookFoodDAO bookFoodDAO;

	public BookFoodDAO getBookFoodDAO() {
		return bookFoodDAO;
	}

	public void setBookFoodDAO(BookFoodDAO bookFoodDAO) {
		this.bookFoodDAO = bookFoodDAO;
	}

	public ErrorMsg addOrUpdateBookFood(BookFood bookFood) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		long foodId = 0;
		if(bookFood.getFoodId() > 0){
			foodId = bookFood.getFoodId();
			long comm_count = commentDAO.getCountByModule(Constant.COMM_BOOK_INFO_MODULE, bookFood.getFoodId());
			long praise_count = praiseDAO.getCountByModule(Constant.COMM_BOOK_INFO_MODULE, bookFood.getFoodId());
			if(bookFood.getType() == Constant.TYPE_BOOK_TICKET){
				comm_count = commentDAO.getCountByModule(Constant.COMM_TICKET_INFO_MODULE, bookFood.getFoodId());
				praise_count = praiseDAO.getCountByModule(Constant.COMM_TICKET_INFO_MODULE, bookFood.getFoodId());
			}
			
			bookFood.setPraiseCount(bookFood.getPraiseBaseCount() + praise_count);
			bookFood.setCommentCount(bookFood.getCommentBaseCount() + comm_count);
			
			bookFoodDAO.updateBookFood(bookFood);
		}else{
			if(null != bookFood && bookFood.getPraiseBaseCount() > 0){
				bookFood.setPraiseCount(bookFood.getPraiseBaseCount());
			}
			if(null != bookFood && bookFood.getCommentBaseCount() > 0){
				bookFood.setCommentCount(bookFood.getCommentBaseCount());
			}
			foodId = bookFoodDAO.addBookFood(bookFood);
		}
		
		if(bookFood.getAdvertStr() != null && !bookFood.getAdvertStr().equals("")){
			Gson gson = new Gson();
			Type type = new TypeToken<Advert>(){}.getType();
			Advert ad = gson.fromJson(bookFood.getAdvertStr(),type);
			if(null != ad && ad.getAdContent() != null && !ad.getAdContent().equals("")){
				advertDAO.deleteAdvert(foodId,Constant.COMM_TICKET_INFO_MODULE);
				
				ad.setModuleId(foodId);
				ad.setModuleType(Constant.COMM_TICKET_INFO_MODULE);
				advertDAO.addAdvert(ad);
			}
			
		}
		
		return msg;
	}
	
	public PageModel getBookFoods(PageModel model,long sellerId,int menuId,String search,long userId,int type) throws Exception{
		return bookFoodDAO.getPagedBookFoods(model, sellerId, menuId, search,userId,type);
	}
	
	public BookFood getBookFoodById(long bookFoodId) throws Exception{
		BookFood food = bookFoodDAO.getById(bookFoodId);
		Advert advert = advertDAO.getAdvert(bookFoodId,Constant.COMM_TICKET_INFO_MODULE);
		if(null != advert)
			food.setAdvert(advert);
		return food;
	}
	
	public void delBookFood(long bookFoodId) throws Exception{
		bookFoodDAO.delBookFood(bookFoodId);
	}
	
	public List<BookFood> getAllBookFoods(int type) throws Exception{
		return bookFoodDAO.listAllInfos(type);
	}
}
